﻿using RockBus.Monitoring.DataContracts;
using RockBus.Monitoring.DataContracts.Configuration;
using RockBus.Monitoring.Interfaces;
using RockBus.PerformanceCounters;
using RockBus.PerformanceCounters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RockBus.Monitoring
{
    public class CollectorManagerConfiguration : ICollectorManagerConfiguration
    {
        public CollectorManagerConfiguration(IPerformanceCounterConfiguration performanceCounterConfiguration)
        {
            this.PerformanceCounterConfiguration = performanceCounterConfiguration;
        }

        private IPerformanceCounterConfiguration PerformanceCounterConfiguration { get; set; }

        public MonitoringConfiguration MonitoringConfiguration
        {
            get
            {
                return new MonitoringConfiguration()
                {
                    CollectionIntervalMs = 1000,
                    BufferSize = 10,
                    PerformanceCounterConfigurations = this.GetPerformanceCounters(),
                };
            }
        }

        public IEnumerable<PerformanceCounterConfigurationBase> GetPerformanceCounters()
        {
            var perfCounters = new List<PerformanceCounterConfigurationBase>()
            {
                new PerformanceCounterInstanceConfiguration()
                {
                    CategoryName = "Processor",
                    CounterName = "% Processor Time",
                    InstanceName = "_Total",
                    AggregationType = AggregationTypeEnum.MovingAverage,
                },
                new PerformanceCounterConfiguration()
                {
                    CategoryName = "Memory",
                    CounterName = "Available MBytes",
                    AggregationType = AggregationTypeEnum.MovingAverage,
                },
            };

            // Add RockBus performance counters
            perfCounters.Add(new PerformanceCounterCategoryConfiguration()
            {
                CategoryName = RockBusPerformanceCounterMetadata.CategoryName,
                AggregationType = AggregationTypeEnum.MovingAverage,
            });
            //foreach (var operationName in this.PerformanceCounterConfiguration.OperationNames)
            //{
            //    foreach (var counterName in RockBusPerformanceCounterMetadata.GetCounterNames(operationName))
            //    {
            //        perfCounters.Add(new PerformanceCounterConfiguration()
            //        {
            //            CategoryName = RockBusPerformanceCounterMetadata.CategoryName,
            //            CounterName = counterName,
            //            AggregationType = AggregationTypeEnum.MovingAverage,
            //        });
            //    }
            //}

            return perfCounters;
        }
    }
}
